package com.example.cardgame

import android.content.Intent
import android.graphics.Bitmap
import android.graphics.Canvas
import android.os.Bundle
import android.provider.MediaStore
import android.util.Log
import android.widget.Button
import android.widget.CheckBox
import android.widget.EditText
import android.widget.ImageView
import android.widget.RadioButton
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.graphics.drawable.toBitmap
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.example.cardgame.GameFrame.Companion.game
import java.io.ByteArrayOutputStream
import java.io.FileInputStream
import java.io.FileOutputStream
import java.io.ObjectInputStream
import java.io.ObjectOutputStream
import java.io.Serializable

data class Information(var name :String,var sex :String,var number:String,var phone: String,var email:String,var hobby:String):Serializable
class InformationActivity : AppCompatActivity() {
    var information:Information?=null
    var checkBox5:String="checkbox"
    var radioGroup:String="radioGroup"
    override fun onCreate(savedInstanceState: Bundle?) {
        val frameLayout=frameLayout()
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_information)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        val sure_button=findViewById<Button>(R.id.sure_button)
        val edit_name=findViewById<EditText>(R.id.edit_name)
        val edit_num=findViewById<EditText>(R.id.edit_num)
        val edit_phone=findViewById<EditText>(R.id.edit_Phone)
        val edit_email=findViewById<EditText>(R.id.edit_EmailAddress)
        val radioButton=findViewById<RadioButton>(R.id.radioButton)
        val radioButton2=findViewById<RadioButton>(R.id.radioButton2)
        if(radioButton.isChecked()){
                radioGroup=radioButton.text.toString()
            }else{
                radioGroup=radioButton2.text.toString()
            }
        val checkbox=findViewById<CheckBox>(R.id.checkBox)
        val checkbox2=findViewById<CheckBox>(R.id.checkBox2)
        val checkbox3=findViewById<CheckBox>(R.id.checkBox3)
        val checkbox4=findViewById<CheckBox>(R.id.checkBox4)

        if(checkbox.isChecked())
            {
                checkBox5=checkbox.text.toString()
            }else if(checkbox2.isChecked()){
                checkBox5=checkbox2.text.toString()
            }else if(checkbox3.isChecked()){
                checkBox5=checkbox3.text.toString()
            }else{
                checkBox5=checkbox4.text.toString()
            }
        val edit_photo=findViewById<Button>(R.id.edit_photo)
        edit_photo.setOnClickListener{
            val takePictureIntent= Intent(MediaStore.ACTION_IMAGE_CAPTURE)
            if(intent.resolveActivity(packageManager)!=null)
            {
                startActivityForResult(takePictureIntent,1)
            }
        }

        val imageView=findViewById<ImageView>(R.id.imageView)
        information=Information(edit_name.text.toString(),radioGroup,edit_num.text.toString(),edit_phone.text.toString(),edit_email.text.toString(),checkBox5)
        sure_button.setOnClickListener{
            Log.i("InformationActivity","$information")
            val intent=Intent()
            intent.putExtra(name,edit_name.text.toString())
            intent.putExtra(num,edit_num.text.toString())
            intent.putExtra(phone,edit_phone.text.toString())
            intent.putExtra(email,edit_email.text.toString())
            intent.putExtra(sex,radioGroup)
            intent.putExtra(hobby,checkBox5)
//            if(radioButton.isChecked()){
//                intent.putExtra(sex,radioButton.getText().toString())
//            }else{
//                intent.putExtra(sex,radioButton2.getText().toString())
//            }
//            if(checkbox.isChecked())
//            {
//                intent.putExtra(hobby,checkbox.getText().toString())
//            }else if(checkbox2.isChecked()){
//                intent.putExtra(hobby,checkbox2.getText().toString())
//            }else if(checkbox3.isChecked()){
//                intent.putExtra(hobby,checkbox3.getText().toString())
//            }else{
//                intent.putExtra(hobby,checkbox4.getText().toString())
//            }
//
            intent.putExtra("bitmap",imageView.drawable.toBitmap())
            setResult(1,intent)
            finish()
        }
        val cancel_button=findViewById<Button>(R.id.cancel_button)
        cancel_button.setOnClickListener{
            finish()
        }
    }
    override fun onStop(){
        super.onStop()
        saveData()
    }
    fun saveData(){
        try {
            val output: FileOutputStream?=openFileOutput("data3",AppCompatActivity.MODE_PRIVATE)
            val objectOutputStream= ObjectOutputStream(output)
            val edit_name=findViewById<EditText>(R.id.edit_name)
            information?.name=edit_name.text.toString()
            Log.i("InformationActivity","${information?.name}")
            val edit_num=findViewById<EditText>(R.id.edit_num)
            information?.number=edit_num.text.toString()
            val edit_phone=findViewById<EditText>(R.id.edit_Phone)
            information?.phone=edit_phone.text.toString()
            val edit_email=findViewById<EditText>(R.id.edit_EmailAddress)
            information?.email=edit_email.text.toString()
            val radioButton=findViewById<RadioButton>(R.id.radioButton)
            val radioButton2=findViewById<RadioButton>(R.id.radioButton2)
            if(radioButton.isChecked()){
                information?.sex=radioButton.text.toString()
            }else{
                information?.sex=radioButton2.text.toString()
            }
            val checkbox=findViewById<CheckBox>(R.id.checkBox)
            val checkbox2=findViewById<CheckBox>(R.id.checkBox2)
            val checkbox3=findViewById<CheckBox>(R.id.checkBox3)
            val checkbox4=findViewById<CheckBox>(R.id.checkBox4)
            if(checkbox.isChecked())
            {
                information?.hobby=checkbox.text.toString()
            }else if(checkbox2.isChecked()){
                information?.hobby=checkbox2.text.toString()
            }else if(checkbox3.isChecked()){
                information?.hobby=checkbox3.text.toString()
            }else{
                information?.hobby=checkbox4.text.toString()
            }
            val imageView=findViewById<ImageView>(R.id.imageView)
//            information?.src=imageView.drawable.toBitmap()
            objectOutputStream.writeObject(information)
            objectOutputStream.close()
            output?.close()
        }catch (e:Exception){
            e.printStackTrace()
        }
    }
    fun loadData():Information?{
        try {
            val input: FileInputStream?=openFileInput("data3")
            val objectInputStream= ObjectInputStream(input)
            val information:Information=objectInputStream.readObject() as Information
            objectInputStream.close()
            input?.close()
            return information
        }catch (e:Exception){
            e.printStackTrace()
            return null
        }
    }
}